package org.biouno.unochoice;

import com.alibaba.fastjson.JSONObject;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.*;
import hudson.model.queue.Tasks;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitTool;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import jenkins.security.FIPS140;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.transport.RemoteConfig;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.GitURIRequirementsBuilder;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.interceptor.RequirePOST;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.regex.Pattern;

import static hudson.Util.fixEmpty;
import static hudson.Util.fixEmptyAndTrim;
import static org.biouno.unochoice.util.Utils.getNodes;

@ExportedBean
public class BuildConfig extends AbstractDescribableImpl<BuildConfig> implements Serializable {


    //编译脚步
    private String script;
    //编译节点
    private List<MultipleChoice> nodeLabels;

    //deb出包配置
    private List<DebConfig> debConfigs;


    @DataBoundConstructor
    public BuildConfig(String script, List<MultipleChoice> nodeLabels, List<DebConfig> debConfigs) {
        this.script = script;
        this.nodeLabels = nodeLabels;
        this.debConfigs = debConfigs;
    }


    @Exported
    @Whitelisted
    public String getScript() {
        return script;
    }

    @Exported
    @Whitelisted
    public List<MultipleChoice> getNodeLabels() {
        return nodeLabels;
    }

    @Exported
    @Whitelisted
    public List<DebConfig> getDebConfigs() {
        return debConfigs;
    }

    public List<String> getNodeLabelsString() {
        List<String> list = new ArrayList<>();
        if (nodeLabels != null) {
            nodeLabels.forEach(nodeLabel -> {
                if (nodeLabel.getNodeLabels() != null) list.add(nodeLabel.getNodeLabels());
            });
        }
        return list;
    }


    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        return jsonObject.toString();
    }


    public JSONObject toJSONObject() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("script", script);
        List<JSONObject> debs = new ArrayList<>();
        List<String>  labels= new ArrayList<>();
        if (nodeLabels != null) {
            nodeLabels.forEach(item -> {
                labels.add(item.getNodeLabels());
            });
        }

        if (debConfigs != null) {
            debConfigs.forEach(debConfig -> {
                debs.add(debConfig.toJSONObject());
            });
        }
        jsonObject.put("nodeLabels",labels);
        jsonObject.put("debConfigs", debs);
        return jsonObject;
    }

    private final static Pattern SCP_LIKE = Pattern.compile("(.*):(.*)");


    @Extension
    public static class DescriptorImpl extends Descriptor<BuildConfig> {

        public ListBoxModel doFillNodeLabelItems(@AncestorInPath Item project) {

            ListBoxModel listBoxModel = new ListBoxModel();
            getNodes().forEach(node -> {
                listBoxModel.add(node);
            });
            return listBoxModel;
        }

        public ListBoxModel doFillNodeLabelsItems(@AncestorInPath Item project) {

            ListBoxModel listBoxModel = new ListBoxModel();
            getNodes().forEach(node -> {
                listBoxModel.add(node);
            });
            return listBoxModel;
        }


        @Override
        public String getDisplayName() {
            return "";
        }
    }
}
